#include "stdafx.h"

extern WSASetLastError_proxy_t pTrueWSASetLastError;

int setsockopt_proxy (
    __in  SOCKET s,
    __in  int level,
    __in  int optname,
    __in  const char *optval,
    __in  int optlen
    )
{
    try
    {
    //SEND
        Header header(GetCurrentThreadId(), "setsockopt");
        setsockopt_in_inout setsockoptIN(s, level, optname, optval, optlen);
        
        std::vector<char> out_buffer;
        Serialize(header, setsockoptIN, out_buffer);

        SendToClient (out_buffer);
        
    //RECEIVE()
        setsockopt_out_inout setsockoptOUT;

        std::vector<char> in_buffer;
        
        RecvFromClient (in_buffer, header);
        Deserialize deserialize(in_buffer);
        deserialize(setsockoptOUT);

        if(setsockoptOUT.wsa_last_error_ != 0)
        {
            pTrueWSASetLastError(setsockoptOUT.wsa_last_error_);
            return SOCKET_ERROR;
        }
        return 0;
    }
    catch (std::exception& ex)
    {
        LOG(plog::error) << "setsockopt: " << ex.what();
    }
    pTrueWSASetLastError (WSAEWOULDBLOCK);
    return SOCKET_ERROR;
}
